package org.rc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.rc.entity.UserRoom;
import org.rc.mapper.UserRoomMapper;
import org.rc.service.UserRoomService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserRoomServiceImpl extends ServiceImpl<UserRoomMapper, UserRoom> implements UserRoomService {

    @Override
    public void addUserRoom(UserRoom userRoom) {
        this.save(userRoom);
    }

    @Override
    public void updateUserRoom(UserRoom userRoom) {
        this.updateById(userRoom);
    }

    @Override
    public UserRoom getBindRecordByRoomIdAndUserId(Long roomId, Long userId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId).eq("user_id",userId)
                .isNotNull("bind_time").isNull("unbind_time");
        return this.getOne(queryWrapper);
    }

    @Override
    public UserRoom getBindRecordByRoomId(Long roomId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId).isNotNull("bind_time").isNull("unbind_time");
        return this.getOne(queryWrapper);
    }

    @Override
    public void deleteByUserId(Long userId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        this.remove(queryWrapper);
    }

    @Override
    public void deleteByRoomId(Long roomId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId);
        this.remove(queryWrapper);
    }

    @Override
    public List<UserRoom> getUserRoomsByRoomIds(List<Long> roomIds) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("room_id",roomIds);
        return this.list(queryWrapper);
    }

    @Override
    public List<UserRoom> getBindingRoomsByRoomIds(List<Long> roomIds) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("room_id",roomIds).isNull("unbind_time");
        return this.list(queryWrapper);
    }

    @Override
    public List<UserRoom> getRoomsByUserId(Long userId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return this.list(queryWrapper);
    }

    @Override
    public List<UserRoom> getBindRoomUserByRoomIds(List<Long> roomIds) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("room_id",roomIds).isNull("unbind_time");
        return this.list(queryWrapper);
    }

    @Override
    public List<UserRoom> getAllRoomsByUserId(Long userId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return this.list(queryWrapper);
    }

    @Override
    public List<UserRoom> getByUserIds(List<Long> userIds) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userIds).isNull("unbind_time");
        return this.list(queryWrapper);
    }

    @Override
    public void updateBatch(List<UserRoom> userRooms) {
        this.updateBatchById(userRooms);
    }

    @Override
    public List<UserRoom> getUserRoomByCondition(Long roomId, Long userId) {
        QueryWrapper<UserRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id",roomId).eq("user_id",userId);
        return this.list(queryWrapper);
    }
}
